<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/30
 * Time: 8:41
 */
namespace app\web\util;

use pscws4\PSCWS4;

class PscwsUtil{
    private $_dict_url;//词典
    private $_rules;//规则
    private $_charset;

    /**
     * @todo 初始化构造方法
     */
    public function __construct($charset = 'utf8')
    {
        $this->_dict_url = app()->getRootPath().'app/common/config/etc/dict.utf8.xdb';
        $this->_rules = app()->getRootPath().'app/common/config/etc/rules.utf8.ini';
        $this->_charset = $charset;
    }

    /**
     * @todo 获取分词结果
     */
    public function get_result($string = ''){
        $pscws = new PSCWS4($this->_charset);
        $pscws->set_dict($this->_dict_url);
        $pscws->set_rule($this->_rules);
        $pscws->send_text($string);
        $tags = $pscws->get_result();//返回所有词分词后的数组
        $pscws->close();

//        print_data($tags);
        return array_response($tags);
    }

    /**
     * @todo 分词返回权重高的词
     * @param string $string
     * @param string $charset
     * @return array
     */
    public function get_tops($string = '',$attr=''){
        $pscws = new PSCWS4($this->_charset);
        $pscws->set_dict($this->_dict_url);
        $pscws->set_rule($this->_rules);
        $pscws->send_text($string);
        $tags = $pscws->get_tops(10, $attr);//返回权重高的词
        $pscws->close();

//        print_data($tags);
        return array_response($tags);
    }
}